package lvbu.wang.lvbuoperator.xmode;

import android.os.Environment;

import com.qing.library.utils.L;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by DaiQing.Zha on 2016/7/28 0028.
 */
public class Mode {

    //java中，一个英文字符占一个字节；字符 "C"用十进制Ascii表示为67；用十六进制表示为43 即0x43

    public final static byte SOH = 0x01; //Xmodem数据头
    public final static byte STX = 0x02; //1k Xmodem数据头
    public final static byte EOT = 0x04; //发送结束
    public final static byte ACK = 0x06; //认可响应
    public final static byte NAK = 0x15; //不认可响应
    public final static byte CAN = 0x18; //撤销传送
    public final static byte EOF = 0x1A; //填充数据包

    public final static int ARR_NUM = 55;
    private void sendData(){

        int dataNo = 1;
        byte[][] data = new byte[ARR_NUM][19];
        String character = "";
        sendBle(data[0]);
        while(dataNo < ARR_NUM){

            if ("05".equals(character)){

                character = "";
                sendBle(data[dataNo]);
                dataNo ++;
            }
            if ("15".equals(character)){
                character = "";
                sendBle(data[dataNo - 1]);
            }
        }
    }
    private void sendBle(byte[] bytes){

        L.e("向蓝牙发送19个字节...");
    }
    public void send(String fileName,boolean useBlock_1k){

        File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
        File sdFile = new File(sdCardDir, fileName);
        try {
            FileInputStream fis = new FileInputStream(sdFile);
            byte[] block;
            if (useBlock_1k){

                block = new byte[1024];
            }else{

                block = new byte[128];
            }
            fis.close();
            sendDataBlocks(fis, 1, block);
            sendEOT();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendEOT() {

    }

    private void sendDataBlocks(FileInputStream fis, int blockNumber, byte[] block) {

    }
}
